/**
 * Advanced Software Engineering (monica Farrow)
 * JUnit ProcessInputLineTestCust class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depotTests;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import depot.*;
import depotModels.*;
import depotAdmin.DuplicateIDException;

public class ProcessInputLineTestCust {

	private CustomerQueue cq;
	private int[] dim1;
	private Parcel pcl1;
	private String inputLine;


	/**
	 * Sets up valid Parcel object for use with Customer objects
	 */
	@Before
	public void setUp() {
		dim1 = new int [] {35,35,34};
		pcl1 = new Parcel("E000100", dim1, 2.55, 2);
	}

	/**
	 * Test element missing from input line
	 * Test only 3 elements
	 * Test for ArrayIndexOutOfBoundsException
	 * @throws DuplicateIDException 
	 * @throws IllegalArgumentException 
	 */
	@Test(expected = ArrayIndexOutOfBoundsException.class)
	public final void testProcInLineElements1_1() throws IllegalArgumentException, DuplicateIDException {
		// create CustomerQueue object
		cq = new CustomerQueue();
		// create input line
		inputLine = "000501,Gregor,McCloud";
		cq.processLine(pcl1,inputLine, 1);
	}


	/**
	 * Test element missing from input line
	 * Test only 3 elements
	 * Test for exception message
	 * @throws DuplicateIDException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public final void testProcInLineElements1_2() throws IllegalArgumentException, DuplicateIDException {
		try {
			// create CustomerQueue object
			cq = new CustomerQueue();
			// create input line
			inputLine = "000501,Gregor,McCloud";
			cq.processLine(pcl1,inputLine, 1);
			fail("Too few elements - should throw exception");
		} catch (ArrayIndexOutOfBoundsException aio) {
			assertTrue(aio.getMessage().contains("3"));
		}
	}


	/**
	 * Test existence of Customer with duplicate ID in custQueue
	 * Test 5th line contains duplicate ID
	 * Test for DuplicateIDException
	 * @throws IllegalArgumentException 
	 * @throws ArrayIndexOutOfBoundsException 
	 * @throws DuplicateIDException 
	 */
	@Test(expected = DuplicateIDException.class)
	public final void testProcInLineDupID1_1() throws ArrayIndexOutOfBoundsException, IllegalArgumentException, DuplicateIDException {
		// create CustomerQueue object
		cq = new CustomerQueue();
		// create input line
		inputLine = "000501,Gregor,McCloud,E000100";
		String inputLine2 = "000102,Frank,Smith,S000101";
		String inputLine3 = "001003,Aliona,Stevens,X000102";
		String inputLine4 = "000104,Most,Luke,E000103";
		String inputLine5 = "000102,Trow,Summat,S000104";
		// create parcels
		int[] dim2 = new int [] {35,35,34};
		Parcel pcl2 = new Parcel("S000101", dim2, 2.55, 2);
		int[] dim3 = new int [] {35,35,34};
		Parcel pcl3 = new Parcel("S000101", dim3, 2.55, 2);
		int[] dim4 = new int [] {35,35,34};
		Parcel pcl4 = new Parcel("S000101", dim4, 2.55, 2);
		int[] dim5 = new int [] {35,35,34};
		Parcel pcl5 = new Parcel("S000101", dim5, 2.55, 2);
		//process input lines
		cq.processLine(pcl1,inputLine, 1);
		cq.processLine(pcl2,inputLine2, 1);
		cq.processLine(pcl3,inputLine3, 1);
		cq.processLine(pcl4,inputLine4, 1);
		cq.processLine(pcl5,inputLine5, 1);
	}

	/**
	 * Test existence of Customer with duplicate ID in custQueue
	 * Test 5th line contains duplicate ID
	 * Test for exception message
	 * @throws IllegalArgumentException 
	 * @throws ArrayIndexOutOfBoundsException 
	 * @throws DuplicateIDException 
	 */
	@Test
	public final void testProcInLineDupID1_2() throws ArrayIndexOutOfBoundsException, IllegalArgumentException, DuplicateIDException {
		try {
			// create CustomerQueue object
			cq = new CustomerQueue();
			// create input line
			inputLine = "000501,Gregor,McCloud,E000100";
			String inputLine2 = "000102,Frank,Smith,S000101";
			String inputLine3 = "001003,Aliona,Stevens,X000102";
			String inputLine4 = "000104,Most,Luke,E000103";
			String inputLine5 = "000102,Trow,Summat,S000104";
			// create parcels
			int[] dim2 = new int [] {35,35,34};
			Parcel pcl2 = new Parcel("S000101", dim2, 2.55, 2);
			int[] dim3 = new int [] {35,35,34};
			Parcel pcl3 = new Parcel("X000102", dim3, 2.55, 2);
			int[] dim4 = new int [] {35,35,34};
			Parcel pcl4 = new Parcel("E000103", dim4, 2.55, 2);
			int[] dim5 = new int [] {35,35,34};
			Parcel pcl5 = new Parcel("S000104", dim5, 2.55, 2);
			//process input lines
			cq.processLine(pcl1,inputLine, 1);
			cq.processLine(pcl2,inputLine2, 1);
			cq.processLine(pcl3,inputLine3, 1);
			cq.processLine(pcl4,inputLine4, 1);
			cq.processLine(pcl5,inputLine5, 1);
			fail("Duplicate ID in 5th line - should throw exception");
		} catch (DuplicateIDException die) {
			assertTrue(die.getMessage().contains("already exists"));
		}
	}

	/**
	 * Test all exceptions above
	 * Test - all elements correct, no dup IDs
	 */
	@Test
	public final void testProcInLineCorrect() {
			// create CustomerQueue object
			cq = new CustomerQueue();
			// create input line
			inputLine = "000501,Gregor,McCloud,E000100";
			String inputLine2 = "000102,Frank,Smith,S000101";
			String inputLine3 = "001003,Aliona,Stevens,X000102";
			String inputLine4 = "000104,Most,Luke,E000103";
			String inputLine5 = "000105,Trow,Summat,S000104";
			// create parcels
			int[] dim2 = new int [] {35,35,34};
			Parcel pcl2 = new Parcel("S000101", dim2, 2.55, 2);
			int[] dim3 = new int [] {35,35,34};
			Parcel pcl3 = new Parcel("X000102", dim3, 2.55, 2);
			int[] dim4 = new int [] {35,35,34};
			Parcel pcl4 = new Parcel("E000103", dim4, 2.55, 2);
			int[] dim5 = new int [] {35,35,34};
			Parcel pcl5 = new Parcel("S000104", dim5, 2.55, 2);
			//process input lines
			try {
			cq.processLine(pcl1,inputLine, 1);
			cq.processLine(pcl2,inputLine2, 1);
				cq.processLine(pcl3,inputLine3, 1);
			cq.processLine(pcl4,inputLine4, 1);
			cq.processLine(pcl5,inputLine5, 1);
			} catch (ArrayIndexOutOfBoundsException | IllegalArgumentException
					| DuplicateIDException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}



}
